import { createSlice } from "@reduxjs/toolkit";
//RTK的切片数据，用与保存多个state数据并给各个组件使用
const loginSlice=createSlice({
    name:'login',
    //初始state值
    initialState:()=>{
        //先从磁盘里读取role
        const localrole=localStorage.getItem('dmrole');
       
        if(!localrole){
            return{
                isLogined:false,
                role:null,
                
            }
        }
        else{
            return{
                isLogined:true,
                role:localrole,
                
            }
        }
        
    },
    reducers:{
        //修改state方法
        setLoginState(state,action){
            
            state.isLogined=true;
            // console.log(action);
            state.role=action.payload;
            localStorage.setItem('dmrole',state.role)

        },
        setLogoutState(state,action){
            
            state.isLogined=false;
            state.role=null;
            localStorage.removeItem('dmrole');

        }
    }
    
});
export default loginSlice;
export const {setLoginState,setLogoutState}=loginSlice.actions;

